package com.dong.rabbitmq.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.amqp.SimpleRabbitListenerContainerFactoryConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

/**
 * <p>
 * ocr mq配置文件
 * </p>
 *
 * @author: DJ
 * @Date: 14:22 2022/9/30
 */
@Configuration
public class OcrMqConfig {

    /**
     * 交换机名称
     */
    public static final String EXCHANGE_NAME = "ocr_exchange";

    /**
     * 路由键名称-发送文件信息给OCR
     */
    public static final String ROUTING_KEY_FILEINFO = "file_info";

    /**
     * 队列名称-发送文件信息给OCR
     */
    public static final String QUEUE_NAME_FILEINFO = "ocr_queue_file_info";

    /**
     * 创建交换机
     *
     * @author admin
     * @since 2020-01-10
     */
    @Bean
    public DirectExchange getDirectExchange() {
        return new DirectExchange(EXCHANGE_NAME);
    }

    /**
     * 创建队列-发送文件信息给OCR
     *
     * @author admin
     * @since 2020-01-10
     */
    @Bean
    public Queue getQueueFileInfo() {
        return new Queue(QUEUE_NAME_FILEINFO, true);
    }

    /**
     * 创建路由键-发送文件信息给OCR
     *
     * @author admin
     * @since 2020-01-10
     */
    @Bean
    public Binding bindingQueueFileInfo() {
        return BindingBuilder.bind(getQueueFileInfo()).to(getDirectExchange()).with(ROUTING_KEY_FILEINFO);
    }


    //    @Bean(name = {"xhConnectionFactory"})
//    public ConnectionFactory xhConnectionFactory() {
//        CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
//        connectionFactory.setHost("10.10.3.109");
//        connectionFactory.setPort(5672);
//        connectionFactory.setUsername("admin");
//        connectionFactory.setPassword("admin");
//        connectionFactory.setVirtualHost("/test");
//        return (ConnectionFactory) connectionFactory;
//    }
    @Bean(name = {"xhConnectionFactory"})
    public ConnectionFactory xhConnectionFactory(@Value("${spring.rabbitmq.xh.host}") String host, @Value("${spring.rabbitmq.xh.port}") int port, @Value("${spring.rabbitmq.xh.username}") String username, @Value("${spring.rabbitmq.xh.password}") String password, @Value("${spring.rabbitmq.xh.virtual-host}") String virtualHost) {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
        connectionFactory.setHost(host);
        connectionFactory.setPort(port);
        connectionFactory.setUsername(username);
        connectionFactory.setPassword(password);
        connectionFactory.setVirtualHost(virtualHost);
        return (ConnectionFactory) connectionFactory;
    }

    @Bean(name = {"xhRabbitTemplate"})
    @Primary
    public RabbitTemplate xhRabbitTemplate(@Qualifier("xhConnectionFactory") ConnectionFactory connectionFactory) {
        RabbitTemplate xhRabbitTemplate = new RabbitTemplate(connectionFactory);
        return xhRabbitTemplate;
    }


    @Bean(name = {"xhContainerFactory"})
    public SimpleRabbitListenerContainerFactory xhFactory(SimpleRabbitListenerContainerFactoryConfigurer configurer, @Qualifier("xhConnectionFactory") ConnectionFactory connectionFactory) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
//        factory.setAcknowledgeMode(AcknowledgeMode.MANUAL);
        configurer.configure(factory, connectionFactory);
        return factory;
    }
}
